package com.beyond.trace.controller;

import com.beyond.trace.domain.BaseYield;
import com.beyond.trace.service.BaseYieldService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName: BaseYieldController
 * @author: z越
 * @date: 2024/4/8  11:24
 * @description
 */
@RestController
@RequestMapping("/base/yield")
@Api(tags = "园区产值对比配置管理")
@RequiredArgsConstructor
public class BaseYieldController extends BaseController {
    private final BaseYieldService baseYieldService;

    @GetMapping
    @ApiOperation("查询列表（分页）")
    public TableDataInfo page(){
        startPage();
        List<BaseYield> list = this.baseYieldService.list();
        return getDataTable(list);
    }

    @GetMapping("/list")
    @ApiOperation("查询列表")
    public AjaxResult list(){
        List<BaseYield> list = this.baseYieldService.list();
        return AjaxResult.success(list);
    }

    @GetMapping("/{id}")
    @ApiOperation("查询详情")
    public AjaxResult getInfo(@PathVariable Long id){
        return AjaxResult.success(this.baseYieldService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody BaseYield req){
        List<BaseYield> list = this.baseYieldService.list();
        int maxNum = 4;
        if (CollectionUtils.isNotEmpty(list) && list.size()==maxNum){
            return AjaxResult.error("最多可以支持四组数据");
        }
        return AjaxResult.success(this.baseYieldService.save(req));
    }

    @PutMapping
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody BaseYield req){
        if (Objects.isNull(req.getId())){
            return AjaxResult.error("id不能为null，请核对后重新操作");
        }
        BaseYield oldPo = this.baseYieldService.getById(req.getId());
        if (Objects.isNull(oldPo)){
            return AjaxResult.error("未查询到相关数据，请核对后重新操作");
        }
        return AjaxResult.success(this.baseYieldService.updateById(req));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    public AjaxResult delete(@PathVariable Long id){
        return AjaxResult.success(this.baseYieldService.removeById(id));
    }
}
